package com.itheima.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.system.RoleDao;
import com.itheima.domain.system.Role;
import com.itheima.service.system.RoleService;
import com.itheima.utils.MysqlUtils;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class RoleServiceImpl implements RoleService {


    // 2021/10/17 start by chenggongbing
    // 角色增删改三层
    @Override
    public PageInfo findByPage(String pageNum) {
        int page = pageNum==null||pageNum=="" ? 1 : Integer.parseInt(pageNum);
        PageHelper.startPage(page,5);
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        RoleDao roleMapper = sqlSession.getMapper(RoleDao.class);
        List<Role> roleList = roleMapper.findAll();
        PageInfo pageInfo = new PageInfo(roleList);
        sqlSession.close();
        return pageInfo;
    }

    @Override
    public void addRole(Role role) {
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        RoleDao roleMapper = sqlSession.getMapper(RoleDao.class);
        role.setRoleId(UUID.randomUUID().toString());
        roleMapper.addRole(role);
        sqlSession.close();
    }

    @Override
    public void updateRole(Role role) {
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        RoleDao roleMapper = sqlSession.getMapper(RoleDao.class);
        //role.setRoleId(UUID.randomUUID().toString());
        //System.out.println(role)
        roleMapper.updateRole(role);
        sqlSession.close();
    }


    @Override
    public void deleteRole(String roleId) {
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        RoleDao roleMapper = sqlSession.getMapper(RoleDao.class);
        roleMapper.deleteRole(roleId);
        sqlSession.close();
    }

    @Override
    public Role findRoleById(String id) {
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        RoleDao roleMapper = sqlSession.getMapper(RoleDao.class);
        Role role = roleMapper.findRoleById(id);
        sqlSession.close();
        return role;
    }



    @Override
    public List<Map> findAuthorDataByRoleId(String roleId) {
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        RoleDao roleMapper = sqlSession.getMapper(RoleDao.class);
        List<Map> authorDataByRid = roleMapper.findAuthorDataByRid(roleId);
        sqlSession.close();
        return authorDataByRid;
    }

    @Override
    public void updateRoleModule(String roleId, String moduleIds) {
        SqlSession sqlSession = MysqlUtils.getSqlSession();
        RoleDao roleMapper = sqlSession.getMapper(RoleDao.class);
        //调用dao层操作

        //修改role_module
        //1.现有的关系全部取消掉
        roleMapper.deleteRoleModule(roleId);
        //2.建立新的关系（多个）
        String[] moduleIdes = moduleIds.split(",");
        for (String moduleId : moduleIdes) {
            roleMapper.saveRoleModule(roleId,moduleId);
        }

    }


}
